//
//  StoryDetailTableViewCell.m
//  BreadTravel
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "StoryDetailTableViewCell.h"

@implementation StoryDetailTableViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return self;
}


-(void)createSubView
{
    self.headImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.headImageView];
    //设置半径
    self.headImageView.layer.cornerRadius = 10;
    //裁掉边框
    self.headImageView.layer.masksToBounds = YES;

    self.label = [[UILabel alloc] init];
    [self.contentView addSubview:self.label];
    self.label.numberOfLines = 0;
    self.label.font = [UIFont systemFontOfSize:16.f];

    
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(@10);
        make.size.mas_equalTo(CGSizeMake(10, 10));
        
    }];
    
    
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(@20);
        make.top.equalTo(self.headImageView.mas_bottom).offset(10);
    }];


    
}


-(void)setModel:(StoryDetailModel *)model
{
    
    
    CGSize sizeImage = [[self class] heightForImageModelWidth:model.photo_width ModelHeight:model.photo_height];
    [self.headImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(@20);
        make.size.mas_equalTo(sizeImage);
        
    }];
    
    NSURL *url = [NSURL URLWithString:model.photo_s];
    [self.headImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@""]];
    
    [self.label mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@20);
        make.right.mas_equalTo(@(-20));
        make.top.equalTo(self.headImageView.mas_bottom).offset(10);
        
    }];
    
    self.label.text = model.text;
    
    
//    CGFloat height = [[self class] heightForContent:self.aStudent.contents];
//    
//    CGRect tempFrame = self.contentsLabel.frame;
//    tempFrame.size.height = height;
//    self.contentsLabel.frame = tempFrame;

    
}


//图片自适应高度   根据请求的数据 等比例计算高度
/**
 *
 *
 *  @param width  网络请求数据 宽
 *  @param height 网络请求数据 高
 *
 *  @return
 */
+(CGSize)heightForImageModelWidth:(CGFloat)width ModelHeight:(CGFloat)height
{
    CGSize size = CGSizeMake(WIDTH - 40 , height / width * (WIDTH - 40));
    return size;
}


+(CGFloat)heightForContent:(NSString *)content
{
    //参数1.设置计算高度按照什么样的size来计算
    //宽度一定与label快读一致，高度尽量大一些
    //参数2.设置计算类型
    //参数3.设置计算内容按照多大的字体计算
    //注意：字体大小也要跟label字体一样
    //参数4.系统备用参数，设置为 nil
    //
    CGSize size = CGSizeMake(300, 1000000);
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17.0], NSFontAttributeName , nil];
    CGRect frame = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return frame.size.height;
}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
